package com.example.myapplication;
import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication.utils.LogUtils;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private TextView mTextView;
    private TextView mTextView1;
    private TextView mTextView3;
    private Button mButton;
    private LinearLayout mLayout;
    private TextView mText;
    private EditText mEditText;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;//123 345
        });
        mTextView = findViewById(R.id.my_text);
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d(TAG, "=============mTextView onClick===============");
                onViewClick(1);
            }
        });
        mTextView1 = findViewById(R.id.my_text1);
        mTextView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d(TAG, "=============mTextView1 onClick===============");
                showToast("text view 2 click");

            }
        });
        mTextView3 = findViewById(R.id.my_text3);
//mTextView3.setText("显示的文字");
//mTextView3.setText(R.string.textView_show);
        mTextView3.setText(getResources().getString(R.string.app_name));
        mTextView3.setVisibility(View.VISIBLE);
//mTextView3.setBackground(getDrawable(R.mipmap.bg_blue));
//mTextView3.setBackgroundResource(R.mipmap.bg_yellow);
        mTextView3.setBackgroundColor(getColor(R.color.black));
        mTextView3.setTextSize(getResources().getDimension(R.dimen.text_size));
        mTextView3.setTextColor(getColor(R.color.white));
        LogUtils.d(TAG, "mTextView3 显示的文字是:" + mTextView3.getText().toString());
        mTextView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(mTextView3.getText().toString());
            }
        });
        mButton = findViewById(R.id.button);
        mButton.setText("1234");
        //mButton.setTextColor(getColor(R.color.color_selector));
        mButton.setTextColor(getColor(R.color.white));
        mButton.setBackgroundColor(getColor(R.color.color_selector));
        //mButton.setBackground(getDrawable(R.drawable.bg_button_selector));
        mButton.setBackgroundResource(R.drawable.bg_button_selector);

        mLayout = findViewById(R.id.main);
        mText = findViewById(R.id.textview);
        mLayout.setOnClickListener(this);
        mText.setOnClickListener(this);
        mButton.setOnClickListener(this);
        mButton.setEnabled(true);
        mEditText = findViewById(R.id.edit);
        //mEditText.setText("你好你好");
        mEditText.requestFocus();
        //mEditText.setSelection(1);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                LogUtils.d(TAG, "=========beforeTextChanged===========" + s.toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LogUtils.d(TAG, "=========onTextChanged===========" + s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                LogUtils.d(TAG, "=========afterTextChanged===========" + s.toString());
                mButton.setText(s.toString());
            }
        });

        mImageView = findViewById(R.id.image);
        //mImageView.setImageResource(R.drawable.radio_selector);
        mImageView.setImageDrawable(getDrawable(R.drawable.radio_selector));
        mImageView.setBackground(getDrawable(R.drawable.ic_launcher_background));
        mImageView.setBackgroundResource(R.drawable.bg_button_selector);
        mImageView.setOnClickListener(this);
    }

    private void onViewClick(int id) {
        LogUtils.e(TAG, "=============onViewClick=============== " + id);
        if (id == 1) {
            showActivity();
        }
    }

    private void showActivity() {
        LogUtils.i(TAG, "=============showActivity===============");
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }
    private void showToast(String text) {
        LogUtils.i(TAG, "=============showToast===============" + text);
        Toast.makeText(this, text, Toast.LENGTH_LONG).show();
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.main) {
            LogUtils.e(TAG, "=============mLayout Click===============");
        } else if (id == R.id.button) {
            LogUtils.i(TAG, "=============mButton Click===============");
        } else if (id == R.id.textview) {
            LogUtils.d(TAG, "=============mText Click===============");
        } else if (id == R.id.image) {
            LogUtils.d(TAG, "=============mImageView Click===============");
        }
    }
}